package com.github.postsanf.yinian.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.CardSyncUploadActivity;
import com.github.postsanf.yinian.activity.ImportPicUploadActivity;
import com.github.postsanf.yinian.bean.YNAlbum;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.utils.ImageOptHelper;
import com.github.postsanf.yinian.utils.YNUrlHelper;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/7/20.
 */
public class ImportSyncUploadAdapter extends BaseAdapter {
    private List<YNAlbum> datas;
    private Context context;
    private LayoutInflater inflater;
    private ImageLoader imageLoader;

    public ImportSyncUploadAdapter(Context context, List<YNAlbum> datas) {
        this.context = context;
        this.datas = datas;
        this.inflater = LayoutInflater.from(context);
        this.imageLoader = ImageLoader.getInstance();
    }


    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int position) {
        return datas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView,
                        ViewGroup parent) {
        YNAlbum ynAlbum = datas.get(position);
        ViewHold viewHold = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.yn_item_syncupload,
                    null);
            viewHold = new ViewHold();
            viewHold.item = (LinearLayout) convertView
                    .findViewById(R.id.sync_item);
            viewHold.name = (TextView) convertView
                    .findViewById(R.id.sync_item_name);
            viewHold.pic = (SimpleDraweeView) convertView
                    .findViewById(R.id.sync_item_pic);
            viewHold.ok = (ImageView) convertView
                    .findViewById(R.id.sync_item_ok);
            convertView.setTag(viewHold);
        } else {
            viewHold = (ViewHold) convertView.getTag();
        }
        String paths = YNUrlHelper.getHomeImgUrl(context, ynAlbum.getGpic());
        Uri uri = Uri.parse(paths);
        ImageOptHelper.showFrescoHome(uri, viewHold.pic, context);
        viewHold.name.setText(ynAlbum.getGname());
        final YNAlbum ynAlbum2 = ynAlbum;
        final int yngroupid = ynAlbum.getGroupid();
        final ImageView okiv = viewHold.ok;
        viewHold.item.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (!ynAlbum2.isSelect()) {
                    ynAlbum2.setSelect(true);
                    okiv.setVisibility(View.VISIBLE);
                    ImportPicUploadActivity.tagList.add(ynAlbum2);
                } else {
                    ynAlbum2.setSelect(false);
                    okiv.setVisibility(View.GONE);
                    for (int i = 0; i < ImportPicUploadActivity.tagList.size(); i++) {
                        if (ImportPicUploadActivity.tagList.get(i).getGroupid() == yngroupid) {
                            ImportPicUploadActivity.tagList.remove(i);
                        }
                    }
                }
//                boolean bb = false;
//                for (int i = 0; i < CardSyncUploadActivity.tagList.size(); i++) {
//                    if (CardSyncUploadActivity.tagList.get(i).getGroupid() == yngroupid) {
//                        bb = true;
//                    }
//                }
//                if (bb) {
//                    Toast.makeText(context, "已选，不要重复选择", Toast.LENGTH_SHORT).show();
//                } else {
//                    CardSyncUploadActivity.tagList.add(ynAlbum2);
//                }
                notifyDataSetChanged();
                context.sendBroadcast(new Intent(ACTIONs.updataSyncUploadok));
            }
        });
//        if (ynAlbum2.isSelect()) {
//            viewHold.ok.setVisibility(View.VISIBLE);
//        } else {
//            viewHold.ok.setVisibility(View.GONE);
//        }
        return convertView;
    }

    class ViewHold {
        private LinearLayout item;
        private SimpleDraweeView pic;
        private ImageView ok;
        private TextView name;
    }


}
